import axios from "axios";
axios.defaults.baseURL = 'http://localhost:3000/api';
export default function ajax(url, data, method = 'GET') {
    if(Object.prototype.toString.call(data) !== '[object Object]'){
        throw new Error('ajax参数data类型错误')
    }
    if(method.toUpperCase() === 'GET'){
        let queryStr = Object.keys(data).reduce((pre,next) => {
            if(pre === ''){
                return pre+`${next}=${data[next]}`;
            }
            return pre+`&${next}=${data[next]}`;

        },'');
        url = `${url}?${queryStr}`;
        return axios.get(url)
    }else if(method.toUpperCase() === 'POST'){
        return axios.post(url,data)
    }else {
        throw new Error('ajax请求方式错误')
    }
}